const app = getApp()
const util = require('../../../utils/util.js')

// 返回处理
const handleRes = (response, fn, url, data, confirmFn) => {
  console.log(url, 'res-->>', response)
  if (response.statusCode == 200) {
    if (response.data.code == 100000) {
      wx.reLaunch({
        url: '../index/index',
      })
      return
    } else if ((response.data.result == 0 || response.data.result === undefined) && typeof fn == 'function') {
      fn(response.data)
      return;
    } else if (response.data.result == 1001) {
      wx.showModal({
        title: '温馨提示',
        confirmColor: '#8290DD',
        content: response.data.description,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
            if (confirmFn) {
              confirmFn(true);
            }
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      })
      return;
    } else {
      wx.showModal({
        title: '温馨提示',
        confirmColor: '#8290DD',
        showCancel: false,
        content: response.data.description
      })
    }
  } else {
    // app.sendLog(url, JSON.stringify(response), JSON.stringify(data));
    wx.showToast({
      title: '服务' + response.statusCode,
      image: '/images/gantan.png',
      duration: 2500
    })
    return;
  }
}

const handleErr = (error, url, data) => {
  console.log(url, 'error-->>', error)
  wx.showToast({
    title: '请求失败请重试',
    image: '/images/gantan.png',
    duration: 2500
  })
}

const handleRequest = (data, fn, url, confirmFn) => {

  handleRequestCallback(data, fn, url, confirmFn)

}

const handleRequestCallback = (data, fn, url, confirmFn) => {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })

  wx.request({
    url: util.CONST_DATA.BASE_URL + url,
    header: header,
    method: method,
    data: data,
    success: function (response) {
      wx.hideLoading()
      handleRes(response, fn, url, data, confirmFn)
    },
    fail: function (error) {
      wx.hideLoading()
      handleErr(error, url, data)
    }
  })
}


const header = app.globalData.header
const method = 'POST'

const lbbInneralTaskService = {

  /**
   * 巡检公司任务
   * 获取公司内部任务列表
   * @param {*} data 
   * @param {*} fun 
   */
  getCompanyTaskList:(data, fun) => {
    let url = 'task/company/list';
    handleRequest(data, fun, url)
  },

  /**
   * 巡检公司任务
   * 获取反馈列表
   * @param {*} data 
   * @param {*} fun 
   */
  getFeedbackRecordList:(data, fun) => {
    let url = 'task/company/feedbackTipList';
    handleRequest(data, fun, url)
  },

  /**
   * 巡检公司任务
   * 获取任务详情
   * @param {*} data 
   * @param {*} fun 
   */
  getTaskDetail:(data, fun) => {
    let url = 'task/company/getByTaskId';
    handleRequest(data, fun, url)
  },

  /**
   * 巡检公司任务
   * 获取任务详情
   * @param {*} data 
   * @param {*} fun 
   */
  getFeedbackTaskDetail:(data, fun) => {
    let url = 'task/company/getByTaskId';
    handleRequest(data, fun, url)
  },

  /**
   * 巡检公司任务
   * 获取反馈配置信息
   * @param {*} data 
   * @param {*} fun 
   */
  getFeedbackRecordDetail:(data, fun) => {
    let url = 'task/company/feedbackConfList';
    handleRequest(data, fun, url)
  },

  /**
   * 巡检公司任务
   * 提交反馈
   */
//   saveFeedbackInfo:(data, fun) => {
//     let url = 'task/company/saveFeedbackInfo';
//     handleRequest(data, fun, url)
//   },

  /**
   * 巡检公司任务
   * 提交反馈
   */
  saveFeedbackInfo:(data,fn) =>{
    wx.showLoading({
        title: '加载中...',
        mask: true
      })
      let url = "task/company/saveFeedbackInfo";
      wx.request({
        url: util.CONST_DATA.BASE_URL +url,
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token') || '',
          'mini_ver':'1.2.1'
        },
        method: method,
        data: data,
        success: function (response) {
          wx.hideLoading()
          handleRes(response, fn, url, data)
        },
        fail: function (error) {
          wx.hideLoading()
          handleErr(error, url, data)
        }
      })
  },

  /**
   * 巡检公司任务
   * 提交反馈
   */
  saveFeedbackInfoBatch:(data,fn) =>{
    wx.showLoading({
        title: '加载中...',
        mask: true
      })
      let url = "task/company/saveFeedbackInfoBatch";
      wx.request({
        url: util.CONST_DATA.BASE_URL +url,
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token') || '',
          'mini_ver':'1.2.1'
        },
        method: method,
        data: data,
        success: function (response) {
          wx.hideLoading()
          handleRes(response, fn, url, data)
        },
        fail: function (error) {
          wx.hideLoading()
          handleErr(error, url, data)
        }
      })
  },
}
 

module.exports = lbbInneralTaskService